﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace _0912089
{
    public class Combobox : ComponentView
    {
        private List<Label> _items = new List<Label>();
        private int _selectedIndex = 0;
        private Color _titleColor;
        private Button _next;

        public Button Next
        {
            get { return _next; }
            set { _next = value; }
        }
        private Button _previous;

        public Button Previous
        {
            get { return _previous; }
            set { _previous = value; }
        }

        private ComponentView _panel;

        private bool isLeftMouseClicked = false;
        private float _titleAlpha = 1;

        public List<Label> Items
        {
            get { return _items; }
            set { _items = value; }
        }        

        public int SelectedIndex
        {
            get { return _selectedIndex; }
            set { _selectedIndex = value; }
        }

        public Combobox(List<string> items, Vector2 position, int width, Color color, float alpha)
        {
            Position = position;
            _titleColor = color;
            _titleAlpha = alpha;

            _next = new Button(@"Resources/button_next", new Vector2(width, 0), 1);
            _next.Position = new Vector2(_next.Position.X - _next.Size.X, 0);
            _previous = new Button(@"Resources/button_previous", new Vector2(0, 0), 1);

            Size = new Vector2(width, _next.Size.Y);
            
            foreach (string str in items)
            {
                addItem(str);
            }

            _panel = new ComponentView(@"Resources/panel1", new Vector2(0,0), new Vector2(width, _next.Size.Y));
           
            if (items.Count != 0)
                _panel.addToSubView(_items[0]);

            _panel.addToSubView(_next);
            _panel.addToSubView(_previous);            

            this.addToSubView(_panel);
        }

        public void addItem(string item)
        {
            Label label = new Label(@"Resources/fontNormal", item, new Vector2(Size.X/2,0), _titleColor, _titleAlpha);
            label.Position = new Vector2(Size.X / 2 - label.Size.X / 2, 0);
            _items.Add(label);
        }

        public override void draw(GameTime gameTime, SpriteBatch spriteBatch, Color color)
        {
            base.draw(gameTime, spriteBatch, color);

            _panel.draw(gameTime, spriteBatch, color);
        }

        public override void update(GameTime gameTime)
        {
            base.update(gameTime);

            _panel.update(gameTime);

            #region Mouse processing
            MouseState ms = Mouse.GetState();
            Vector2 mousePosition  = new Vector2(ms.X,ms.Y);
            if (ms.LeftButton == ButtonState.Pressed)
            {
                isLeftMouseClicked = true;
            }
            if (ms.LeftButton == ButtonState.Released && isLeftMouseClicked)
            {
                isLeftMouseClicked = false;
                if(_next.isContainPoint(mousePosition))
                    clickedNextButton();
                if (_previous.isContainPoint(mousePosition))
                    clickedPreviousButton();
            }
            #endregion
        }

        public void clickedNextButton()
        {
            if (_selectedIndex == Items.Count - 1)
                return;
            _selectedIndex++;
            _panel.removeSubViewAtIndex(0);
            _panel.addToSubViewAtIndex(_items[_selectedIndex], 0);
        }

        public void clickedPreviousButton()
        {
            if (_selectedIndex == 0)
                return;
            _selectedIndex--;
            _panel.removeSubViewAtIndex(0);
            _panel.addToSubViewAtIndex(_items[_selectedIndex], 0);
        }
    }
}
